package com.bwie.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.MobileMessageUtil;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 31803
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-07-15 10:45:13
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public R sendCode(String mobile) {
        boolean mobile1 = Validator.isMobile(mobile);
        if(!mobile1){
            return R.error().setMsg("手机号格式不正确");
        }
        String code = RandomUtil.randomNumbers(4);
        redisTemplate.opsForValue().set(mobile,code,5, TimeUnit.MINUTES);
        MobileMessageUtil.sendMobileMessage(mobile,code);
        return R.ok().setMsg("验证码发送成功").setData(code);
    }

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        TbUser tbUser = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserMobile, loginInfoVo.getUserMobile())
        );
        if(tbUser==null){
            String rediscode = redisTemplate.opsForValue().get(loginInfoVo.getUserMobile());
            if(rediscode==null){
                return R.error().setMsg("验证码过期");
            }
            if(!rediscode.equals(loginInfoVo.getUserCode())){
                return R.error().setMsg("用户验证码错误");
            }
            TbUser tbUser1 = new TbUser();
            tbUser1.setUserName("匿名登录");
            tbUser1.setUserPass("123");
            tbUser1.setUserMobile(loginInfoVo.getUserMobile());
            tbUserMapper.insert(tbUser1);
            return R.ok().setMsg("用户注册成功请继续登录").setData(1);
        }
        String rediscode = redisTemplate.opsForValue().get(loginInfoVo.getUserMobile());
        if(rediscode==null){
            return R.error().setMsg("验证码过期");
        }
        if(!rediscode.equals(loginInfoVo.getUserCode())){
            return R.error().setMsg("用户验证码错误");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", tbUser.getUserId())
                .claim("userMobile", tbUser.getUserMobile())
                .compact();
        redisTemplate.opsForValue().set(token,token,5,TimeUnit.HOURS);
        tbUser.setToken(token);
        tbUser.setUserPass(null);

        return R.ok().setData(tbUser);
    }
}




